package com.qqs.eq;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.plaf.synth.SynthSeparatorUI;

import com.alibaba.fastjson.JSON;

public final class StatusPublish {
	private static Map<String, Map<String, String>> lightmap = new HashMap<String, Map<String, String>>();
	private static Map<String, Map<String, String>> tempmap = new HashMap<String, Map<String, String>>();
	private static Map<String, Map<String, String>> ledmap = new HashMap<String, Map<String, String>>();
	private static Map<String, Map<String, String>> eemmap = new HashMap<String, Map<String, String>>();
	private static Map<String, Map<String, String>> sensormap = new HashMap<String, Map<String, String>>();
	private static Map<String, Map<String, String>> cleanmap = new HashMap<String, Map<String, String>>();
	private static List<Watcher> list = new ArrayList<Watcher>();

	public static synchronized void light(String chipid, Map shuxingmap) {
		if (lightmap.get(chipid) == null || lightmap.get(chipid).hashCode() != shuxingmap.hashCode()) {
			lightmap.put(chipid, shuxingmap);
			notifyWatchers("ligth", chipid, shuxingmap);
		}
	}

	public static synchronized void sensor(String chipid, Map shuxingmap) {
		if (sensormap.get(chipid) == null || sensormap.get(chipid).hashCode() != shuxingmap.hashCode()) {
			sensormap.put(chipid, shuxingmap);
			notifyWatchers("sensor", chipid, shuxingmap);
		}
	}

	public static synchronized void temp(String chipid, Map shuxingmap) {
		if (tempmap.get(chipid) == null || tempmap.get(chipid).hashCode() != shuxingmap.hashCode()) {
			tempmap.put(chipid, shuxingmap);
			notifyWatchers("temp", chipid, shuxingmap);
		}
	}

	public static void led(String chipid, Map shuxingmap) {
		if (ledmap.get(chipid) == null || ledmap.get(chipid).hashCode() != shuxingmap.hashCode()) {
			ledmap.put(chipid, shuxingmap);
			notifyWatchers("led", chipid, shuxingmap);
		}
	}

	public static void eem(String chipid, Map shuxingmap) {
		if (eemmap.get(chipid) == null || eemmap.get(chipid).hashCode() != shuxingmap.hashCode()) {
			eemmap.put(chipid, shuxingmap);
			notifyWatchers("eem", chipid, shuxingmap);
		}
	}

	public static synchronized void clean(String chipid, Map shuxingmap) {
		if (cleanmap.get(chipid) == null || cleanmap.get(chipid).hashCode() != shuxingmap.hashCode()) {
			cleanmap.put(chipid, shuxingmap);
			notifyWatchers("clean", chipid, shuxingmap);
		}
	}

	public static synchronized void switchs(String chipid, String location) {
		System.out.print(location);
		for (Watcher watcher : list)
			watcher.singleSwitchData(chipid, location);
	}

	public static synchronized void addWatcher(Watcher watcher) {
		list.add(watcher);
	}

	public static synchronized void removeWatcher(Watcher watcher) {
		list.remove(watcher);
	}

	public static synchronized void notifyWatchers(String type, String chipid, Map map) {
		// 自动调用实际上是主题进行调用的
		if ("ligth".equals(type)) {
			for (Watcher watcher : list)
				watcher.singleLightData(chipid, map);
		} else if ("sensor".equals(type)) {
			for (Watcher watcher : list)
				watcher.singleSensorData(chipid, map);
		} else if ("temp".equals(type)) {
			for (Watcher watcher : list)
				watcher.singleTempData(chipid, map);
		} else if ("led".equals(type)) {
			for (Watcher watcher : list)
				watcher.singleLedData(chipid, map);
		} else if ("eem".equals(type)) {
			for (Watcher watcher : list)
				watcher.singleEemData(chipid, map);
		} else if ("clean".equals(type)) {
			for (Watcher watcher : list)
				watcher.singleCleanData(chipid, map);
		}
	}

	public static Map<String, Map<String, String>> getLightmap() {
		return lightmap;
	}

	public static Map<String, Map<String, String>> getTempmap() {
		return tempmap;
	}

	public static Map<String, Map<String, String>> getLedmap() {
		return ledmap;
	}

	public static Map<String, Map<String, String>> getEemmap() {
		return eemmap;
	}

	public static Map<String, Map<String, String>> getSensormap() {
		return sensormap;
	}
}
